<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class NoticeSetService extends Service{

    public static function indexHeader(){
        $header = array(
        		'id'            =>array('text' => 'ID', 'width' => '70px'),
        		'server_id'   => array('text' => '同步渠道','width' => '300px'),
        		'title'         => array('text' => '标题', 'width' => '150px'),
                'content'       => array('text' => '内容', 'width' => '350px'),
        		//'synchro_type'  => array('text' => '同步方式', 'width'=> '100px'),
        		'type'         => array('text' => '公告位置', 'width' => '100px'),
        		'is_eject'      => array('text' => '是否弹出', 'width'=> '100px'),
                //'item'          => array('text' => '奖励', 'width' => '250px'),  
        		//'receive_time'  => array('text' => '可领取时间', 'width' => '200px'), 
        		'create_user'       => array('text' => '添加人'),
        		'create_time'   => array('text' => '创建时间','width' => '100px'),
        		'synchro_time'  => array('text' => '同步时间','width' => '100px'),
                'action'        => array('text' => '操作', 'width' => '100px'),
        );

        return $header;
    }

    public static function indexData($params){
    	$user = AccAuth::me();
    	if($user['IsAdmin'] != 1 && $user['AddBy'] != 1){
    		$params['create_user'] = $user['UserID'];
    	}
    	
    	$modNoticeSet = new ModNoticeSet();
    	$result = $modNoticeSet->select($params);
        
//    	$synchroTypeList = self::synchroTypeList();
    	$isEjectList = self::isEjectList();
    	$isBindList = self::isBindList();
    	
    	$game = PubBase::getCurrentGame();
    	$params['g'] = $game;
    	$channelList = ChannelService::getChannel($params);

    	$userList = self::userList();
    	$typeList = self::typeList();
        foreach($result as &$v){
//        	$synchro_type = $v['synchro_type'];
        	$item_array = json_decode($v['item'],true);
        	$item = '';
        	foreach ($item_array as $key=>$val){
        		$bind = $isBindList[$val['is_bind']];
        		$item .= $val['item_name'].'-'.$val['item_id'].'-'.$val['item_number'].'-'.$bind;
        		$item .= '</br>';
        	}
        	$v['content'] = '<div style="white-space:normal;">'.strip_tags($v['content']).'</div>';
        	$v['item'] = $item;
        	$v['receive_start_time'] = date('Y-m-d', $v['receive_start_time']);
        	$v['receive_end_time'] = date('Y-m-d', $v['receive_end_time']);        	
        	$v['receive_time'] = $v['receive_start_time'].'——'.$v['receive_end_time'];
//        	$v['synchro_type'] = $synchroTypeList[$v['synchro_type']];
        	$v['is_eject'] = $isEjectList[$v['is_eject']];
        	$v['create_user'] = $userList[$v['create_user']];
        	$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        	
        	
        	$server_array = explode(',', $v['server_id']);
        	$server_name = '';
        	foreach ($server_array as $key=>$val){
        		$server_name .= ','.$channelList[$val];
        	}
        	$server_name = substr($server_name, 1);
        	$v['server_id'] = '<div style="white-space:normal;">'.$server_name.'</div>';
        	
        	$type_array = explode(',', $v['type']);
        	$types = '';
        	foreach ($type_array as $key=>$val){
        		$types .= ','.$typeList[$val];
        	}
        	$types = substr($types, 1);
        	
        	$v['type'] = $types;
         	$id = $v['id'];
//         	if($v['is_synchro'] == 1 && $synchro_type == 1){
//         		$v['server_name'] = '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >同步</a>';
//         		$v['synchro_time'] = '未同步';
//         		$checkbox = '<input value="'.$v['id'].'" name="id" type="checkbox">&nbsp;&nbsp;';
//         	}
        	 
//         	if($v['is_synchro'] == 1 && $synchro_type == 2){
//         		$v['server_name'] = '待自动同步,服务器为'.$v['server_name'];
//         		$v['synchro_time'] = '未自动同步';
//         		$checkbox = '<input value="'.$v['id'].'" name="id" disabled="disabled" type="checkbox">&nbsp;&nbsp;';
//         	}
        	 
//         	if($v['is_synchro'] == 2){
//         		$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
//         		$checkbox = '<input value="'.$v['id'].'" name="id" disabled="disabled" type="checkbox">&nbsp;&nbsp;';
//         	}   

         	if($v['synchro_time'] == ''){
         		$v['server_id'] = '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >同步</a>';
         		$v['synchro_time'] = '未同步';
         		$checkbox = '<input value="'.$v['id'].'" name="id" type="checkbox">&nbsp;&nbsp;';
         		 
         	}else{
         		$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
         		$checkbox = '<input value="'.$v['id'].'" name="id" disabled="disabled" type="checkbox">&nbsp;&nbsp;';
         	}
        	
         	$v['id'] = $checkbox.$v['id'];
         	$v['action']  = ' <a href="'.PubFunc::buildUrl('/oss/?r=noticeSet/edit').'&id='.$id.'">编辑</a>';
        	$v['action'] .= ' <a href="'.PubFunc::buildUrl('/oss/?r=noticeSet/delete').'&id='.$id.'" confirm="是否要删除该条信息?">删除</a>';
        }
        unset($v);

        return $result;
    }
    
    
    public static function doAdd($params){
    	if(PubFunc::checkEmpty($params, array( 'title'))){
    		return array('state' => false, 'msg' => '缺少必要参数.');
    	}
    	
    	$type = $params['type'];
    	$types = '';
    	foreach ($type as $key=>$val){
    		$types .= ','.$val;
    	}
    	$types = substr($types, 1);

    	$user = AccAuth::me();
    	$modNoticeSet = new ModNoticeSet();
    	
    	$person_item = array();
    	foreach ($params['item_id'] as $key=>$val){
    		$data = array();
    		$data['item_name'] = $params['item_name'][$key];
    		$data['item_id'] = $params['item_id'][$key];
    		$data['item_number'] = $params['item_number'][$key];
    		$data['is_bind'] = $params['is_bind'][$key];
    		array_push($person_item, $data);
    	}
    	$item = json_encode($person_item);
    	
    	$modNoticeSet->platform      = 'test';
    	$modNoticeSet->game          = $params['g'];
    	$modNoticeSet->title         = $params['title'];
    	$modNoticeSet->content       = $params['content'];
//    	$modNoticeSet->synchro_type  = $params['synchro_type'];
    	$modNoticeSet->is_eject      = $params['is_eject'];
    	$modNoticeSet->item          = $item;
    	$modNoticeSet->receive_start_time  = strtotime($params['receive_start_time']);
    	$modNoticeSet->receive_end_time    = strtotime($params['receive_end_time']);
        $modNoticeSet->create_user   = $user['UserID'];
        $modNoticeSet->create_time   = time();
        $modNoticeSet->update_user   = $user['UserID'];
        $modNoticeSet->update_time   = time();
        $modNoticeSet->type   = $types;
//        $modNoticeSet->is_synchro      = 1;
//         if($params['synchro_type'] == 2){
//         	$synchro_time = strtotime($params['synchro_time']);
//         	$modNoticeSet->synchro_time   = $synchro_time;
        	
//         	$serverList = PubServer::getOptions($platform, $game, 'id');
//         	$server_id = '';
//         	$server_name = '';
//         	foreach ($params['server'] as $k=>$v){
//         		$server_id .= ','.$v;
//         		$server_name .= ','.$serverList[$v];
//         	}
//         	if($server_id != '' && $server_name != ''){
//         		$server_id = substr($server_id, 1);
//         		$server_name = substr($server_name, 1);
//         		$modNoticeSet->server_id = $server_id;
//         		$modNoticeSet->server_name = $server_name;
//         	}
        	
//         }
    
    	$result = $modNoticeSet->save();
    	return array('state' => $result, 'msg' =>'添加成功');   	 
    }
    
    public static function get($id){
    	$modNoticeSet = new ModNoticeSet();
    	$obj = $modNoticeSet->findByPk($id);
    
    	if($obj){
    		$info = $obj->getAttributes();
    		return $info;
    	}else{
    		return false;
    	}
    }
    
    public static function doEdit($id, $params){
    	if(empty($id)) return false;
    
    	$user = AccAuth::me();
    	
    	$type = $params['type'];
    	$types = '';
    	foreach ($type as $key=>$val){
    		$types .= ','.$val;
    	}
    	$types = substr($types, 1);
    
    	$modNoticeSet = new ModNoticeSet();
    	$update = array();
    	$update['title']         = $params['title'];
    	$update['content']       = trim($params['content']);
    	$update['is_eject']      = $params['is_eject'];
    
    	$update['update_user']   = $user['UserID'];
    	$update['update_time']   = time(); 
    	$update['type']   = $types;
    
    	$modNoticeSet->updateByPk($id, $update);
    	return true;
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modNoticeSet = new ModNoticeSet();
    	$modNoticeSet->deleteByPk($id);
    	return true;
    }
    
    public static function synchro($ids,$servers){
    	$modNoticeSet = new ModNoticeSet();
    	
    	$game = PubBase::getCurrentGame();
    	$params['g'] = $game;
    	$channelList = ChannelService::getChannel($params);
    
    	$ids_array = explode(',', $ids);
    
    	$server_array = explode(',', $servers);
    	$server_array = array_unique($server_array);
    
    	$info_res = '';
    	foreach ($ids_array as $key=>$val){
    		$obj = $modNoticeSet->findByPk($val);
    		$info = $obj->getAttributes();

    		$params = array();
    		$info_res .= '公告设置ID为 <font color="red">'.$info['id'].'</font><br/>';
    		$server_id = '';
    		$server_name = '';
    		foreach ($server_array as $key1=>$val1){
    			$result['state'] = true;
    			if($result['state']){
    				$server_id .= ','.$val1;
    				$info_res .= ' 同步渠道 <font color="red">'.$channelList[$val1].' </font>成功<br/>';
    			}else{
    				$info_res .= ' 同步渠道 <font color="red">'.$channelList[$val1].' </font>失败，失败原因'.$result['msg'].'<br/>';
    			}
    		}
    
    		if($server_id != ''){
    			$server_id = substr($server_id, 1);
    			$update = array();
    			$update['server_id']     = $server_id;
    			$update['synchro_time']  = time();
    			$modNoticeSet->updateByPk($info['id'], $update);
    		}
    	}
    	return $info_res;
    
    }
    
    public static function synchro1($ids,$servers){
    	$modNoticeSet = new ModNoticeSet();
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	 
    	$ids_array = explode(',', $ids);
    	 
    	$server_array = explode(',', $servers);
    	$server_array = array_unique($server_array);
    	 
    	$info_res = '';
    	foreach ($ids_array as $key=>$val){
    		$obj = $modNoticeSet->findByPk($val);
    		$info = $obj->getAttributes();
//     		if($info['synchro_type'] == 2){ //自动同步不能进来这里
//     			continue;
//     		}
    		$params = array();
    		//$params['content'] = $info['message_content'];
    		//$params['type'] = explode(',',$info['message_type']);
    		$info_res .= '公告设置ID为 <font color="red">'.$info['id'].'</font><br/>';
    		$server_id = '';
    		$server_name = '';
    		foreach ($server_array as $key1=>$val1){
    			//$params['server'] = $val1;
    			//$result = self::send($params);
    			$result['state'] = true;
    			if($result['state']){
    				$server_id .= ','.$val1;
    				//$server_name .= ','.$serverList[$val1];
    				$info_res .= ' 同步服务器 <font color="red">'.$serverList[$val1].' </font>成功<br/>';
    			}else{
    				$info_res .= ' 同步服务器 <font color="red">'.$serverList[$val1].' </font>失败，失败原因'.$result['msg'].'<br/>';
    			}
    		}
    		
    		if($server_id != ''){
    			$server_id = substr($server_id, 1);
    			//$server_name = substr($server_name, 1);
    			$update = array();
    			$update['server_id']     = $server_id;
    			//$update['server_name']     = $server_name;
    			$update['synchro_time']  = time();
    			//$update['is_synchro'] = 2;
     			$modNoticeSet->updateByPk($info['id'], $update);
    		}
    	}
    	return $info_res;
    	 
    }

    public static function synchroTypeList(){
    	return array(
    			'1' => '手动同步',
    			'2' => '自动同步'
    	);
    }
    
    public static function isEjectList(){
    	return array(
    			'0' => '不弹出',
    			'1' => '弹出'
    	);
    }
    
    public static function isBindList(){
    	return array(
    			'1' => '绑定',
    			'0' => '不绑定'
    	);
    }
    
    public static function typeList(){
    	return array(
    			'1' => '游戏内',
    			'2' => '选服页'
    	);
    }

    /**
     * 自动同步方法，暂时不需要
     * @return string
     */
    public static function timimg(){
    	$modNoticeSet = new ModNoticeSet();
    	$params = array();
    	echo strtotime(date('Y-m-d H:i',time()));
    	$params['g'] = 'mango';
    	$params['synchro_time'] = strtotime(date('Y-m-d H:i',time()));
    	$params['synchro_type'] = 2;
    	$data = $modNoticeSet->select($params);
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	foreach ($data as $key=>$val){
    		$info_res .= '公告设置ID为 '.$info['id'];
    		$server_id = '';
    		$params = array();
    		$server_array = explode(',', $val['server_id']);
    		foreach ($server_array as $key1=>$val1){
    			//$params['server'] = $val1;
    			//$result = self::send($params);
    			$result['state'] = true;
    			if($result['state']){
    				$server_id .= ','.$val1;
    				$info_res .= ' 自动发送服务器 '.$serverList[$val1].' 成功 \n';
    			}else{
    				$info_res .= ' 自动发送服务器 '.$serverList[$val1].' 失败，失败原因'.$result['msg'].' \n';
    			}
    		}
    		
    		if($server_id != '' && $server_name != ''){
    			$update = array();
    			$update['is_synchro'] = 2;
    			$modNoticeSet->updateByPk($info['id'], $update);
    		}
    	}
    	
    	return $info_res;
    }

}